package jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.SortingParams;

public class TestList {

    public static void main(String[] args) {
//        Jedis jedis = new Jedis("127.0.0.1", 6379);
        Jedis jedis = JedisUtil.getJedis();

        jedis.flushDB();
        System.out.println("===========添加一个list===========");
        jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");
        jedis.lpush("collections", "HashSet");
        jedis.lpush("collections", "TreeSet");
        jedis.lpush("collections", "TreeMap");
        // collections的内容：[TreeMap, TreeSet, HashSet, LinkedHashMap, WeakHashMap, HashMap, Stack, Vector, ArrayList]
        System.out.println("collections的内容："+jedis.lrange("collections", 0, -1));//-1代表倒数第一个元素，-2代表倒数第二个元素,end为-1表示查询全部
        // collections区间0-3的元素：[TreeMap, TreeSet, HashSet, LinkedHashMap]
        System.out.println("collections区间0-3的元素："+jedis.lrange("collections",0,3));

        System.out.println("===============================");
        // 删除列表指定的值 ，第二个参数为删除的个数（有重复时），后add进去的值先被删，类似于出栈
        /**
         * count > 0: 从头往尾移除count个值为 value 的元素。
         * count < 0: 从尾往头移除count个值为 value 的元素。
         * count = 0: 移除所有值为 value 的元素。
         */
        System.out.println("删除指定元素个数："+jedis.lrem("collections", 2, "HashMap")); //删除指定元素个数：1
        System.out.println("collections的内容："+jedis.lrange("collections", 0, -1)); // collections的内容：[TreeMap, TreeSet, HashSet, LinkedHashMap, WeakHashMap, Stack, Vector, ArrayList]
        System.out.println("删除下表0-3区间之外的元素："+jedis.ltrim("collections", 0, 3)); //删除下表0-3区间之外的元素：OK
        System.out.println("collections的内容："+jedis.lrange("collections", 0, -1)); //collections的内容：[TreeMap, TreeSet, HashSet, LinkedHashMap]
        System.out.println("collections列表出栈（左端）："+jedis.lpop("collections")); //collections列表出栈（左端）：TreeMap
        System.out.println("collections的内容："+jedis.lrange("collections", 0, -1)); //collections的内容：[TreeSet, HashSet, LinkedHashMap]
        System.out.println("collections添加元素，从列表右端，与lpush相对应："+jedis.rpush("collections", "EnumMap")); //collections添加元素，从列表右端，与lpush相对应：4
        System.out.println("collections的内容："+jedis.lrange("collections", 0, -1)); //collections的内容：[TreeSet, HashSet, LinkedHashMap, EnumMap]
        System.out.println("collections列表出栈（右端）："+jedis.rpop("collections")); //collections列表出栈（右端）：EnumMap
        System.out.println("collections的内容："+jedis.lrange("collections", 0, -1)); //collections的内容：[TreeSet, HashSet, LinkedHashMap]
        System.out.println("修改collections指定下标1的内容："+jedis.lset("collections", 1, "LinkedArrayList")); //修改collections指定下标1的内容：OK
        System.out.println("collections的内容："+jedis.lrange("collections", 0, -1)); //collections的内容：[TreeSet, LinkedArrayList, LinkedHashMap]
        System.out.println("===============================");
        System.out.println("collections的长度："+jedis.llen("collections")); //collections的长度：3
        System.out.println("获取collections下标为2的元素："+jedis.lindex("collections", 2)); //获取collections下标为2的元素：LinkedHashMap
        System.out.println("===============================");
        jedis.lpush("sortedList", "3","6","2","0","7","4");
        System.out.println("sortedList排序前："+jedis.lrange("sortedList", 0, -1)); //sortedList排序前：[4, 7, 0, 2, 6, 3]
        System.out.println(jedis.sort("sortedList")); //[0, 2, 3, 4, 6, 7]
        System.out.println("sortedList升序排序后："+jedis.lrange("sortedList", 0, -1)); //sortedList排序后：[4, 7, 0, 2, 6, 3]


        SortingParams sortingParams = new SortingParams();
        sortingParams.desc();
        System.out.println(jedis.sort("sortedList", sortingParams)); //[7, 6, 4, 3, 2, 0]
        System.out.println("sortedList降序序排序后："+jedis.lrange("sortedList", 0, -1)); //sortedList降序序排序后：[4, 7, 0, 2, 6, 3]

        jedis.close();
    }

}
